//
//  LoginController.m
//  ZZmetro
//
//  Created by 一介布衣 on 2020/6/5.
//  Copyright © 2020 一介布衣. All rights reserved.
//

#import "LoginController.h"
#import "LoginRequest.h"
#import "LaunchHandler.h"
#import <React/RCTRootView.h>

@interface LoginController ()
@property (weak, nonatomic) IBOutlet UIView *phoneView;
@property (weak, nonatomic) IBOutlet UIView *pwdView;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwdTextField;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;

@end

@implementation LoginController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    self.phoneView.backgroundColor = HEXCOLOR(0XEEEEEE);
    self.pwdView.backgroundColor = HEXCOLOR(0XEEEEEE);
    
    [self.loginBtn.layer setMasksToBounds:YES];
    [self.loginBtn.layer setCornerRadius:4];
    [self.loginBtn setBackgroundColor:COLOR_VIEW_6];
    [self.pwdTextField setSecureTextEntry:YES];
    
    [self.pwdTextField addTarget:self action:@selector(passWordFaildChange:) forControlEvents:(UIControlEventEditingChanged)];
    
    [self.pwdTextField addTarget:self action:@selector(numberFaildChange:) forControlEvents:(UIControlEventEditingChanged)];
    
    [self checkPhoneNubAndPwd];
}

- (void)checkPhoneNubAndPwd{
    
    NSString* phone = self.phoneTextField.text;
    NSString* pwd = self.pwdTextField.text;
    
    if(pwd.length >= 1){
        self.loginBtn.backgroundColor=COLOR_VIEW_1;
        self.loginBtn.enabled = YES;
    }else{
        self.loginBtn.backgroundColor=COLOR_VIEW_6;
        self.loginBtn.enabled = NO;
    }
    
}

-(void)passWordFaildChange:(UITextField *)textField
{
    if (textField == self.pwdTextField) {
        static NSUInteger maxLength =20;
        
        NSString *inputText = textField.text;
        
        //获取高亮部分
        UITextRange *markedTextRange = [textField markedTextRange];
        UITextPosition *position = [textField positionFromPosition:markedTextRange.start offset:0];
        
        //没用高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position || !markedTextRange) {
            if (inputText.length > maxLength) {
                NSRange rangeIndex = [inputText rangeOfComposedCharacterSequenceAtIndex:maxLength];
                if (rangeIndex.length == 1) {
                    textField.text = [inputText substringToIndex:maxLength];
                } else {
                    NSRange range = [inputText rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
                    textField.text = [inputText substringWithRange:range];
                }
            }
            
        }
    }
    
    [self checkPhoneNubAndPwd];
}
-(void)numberFaildChange:(UITextField *)textField{
    if (textField == self.phoneTextField) {
        static NSUInteger maxLength = 20;
        
        NSString *inputText = textField.text;
        
        //获取高亮部分
        UITextRange *markedTextRange = [textField markedTextRange];
        UITextPosition *position = [textField positionFromPosition:markedTextRange.start offset:0];
        
        //没用高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position || !markedTextRange) {
            if (inputText.length > maxLength) {
                NSRange rangeIndex = [inputText rangeOfComposedCharacterSequenceAtIndex:maxLength];
                if (rangeIndex.length == 1) {
                    textField.text = [inputText substringToIndex:maxLength];
                } else {
                    NSRange range = [inputText rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
                    textField.text = [inputText substringWithRange:range];
                }
            }
            
        }
    }
    
    [self checkPhoneNubAndPwd];
}


#pragma mark initView
-(void)initWithView
{
   
}

#pragma mark 登录
- (IBAction)loginClick:(id)sender {
     //[Toast spin:nil];
    
    //TODO
    if (1 == 1) {
        [self openRnPage];
        return;
    }
    
    /*[[HttpRequestManager sharedInstance] requestCookie:BASE_URL callback:^(HttpResponse * _Nullable response) {
        [Toast hidden];
        if (response.code == 0) {
            //[Toast toast: response.obj];
            Session.cookie = response.obj;
            
            [NetWorkConfig initNetWork];
            
            LoginRequest* request = [[LoginRequest alloc]init];
            request.userName = @"101771";
            request.password = @"ZZdt@101771";
            [HttpRequestOperation request:request quite:NO callBack:^(HttpResponse * _Nullable response) {
                //[Toast toast:response.msg];
                [LaunchHandler toHomeController];
            }];
        } else {
            [Toast toast:@"登录失败"];
        }
    }];*/
    
    
}

- (void) openRnPage {
    NSLog(@"High Score Button Pressed");
    NSURL *jsCodeLocation = [NSURL URLWithString:@"http://192.168.3.208:8081/index.bundle?platform=ios"];
    //NSURL *jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"index" withExtension:@"jsbundle"];


    RCTRootView *rootView = [[RCTRootView alloc] initWithBundleURL: jsCodeLocation
                                      moduleName: @"MyReactNativeApp"
                               initialProperties: nil
                                   launchOptions: nil];
        UIViewController *vc = [[UIViewController alloc] init];
        vc.view = rootView;
        //[self presentViewController:vc animated:YES completion:nil];
    
        UINavigationController *navCtl = [UIViewController currentNavigationController];
        [navCtl pushViewController:vc animated:YES];
}


@end
